package com.neko.Thread_Learning;

/** 13、观察【测试线程の状态】
 * ----------------------
 *
 * */
public class t13_Thread_State {

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(() -> {

            for (int i=0; i<5; i++) {   // 总共等待5s
                try {
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("/////////");

        });


        // 观察 Thread 状态
        Thread.State state = thread.getState();
        System.out.println(state);

        // 观察启动后
        thread.start();
        state = thread.getState();
        System.out.println(state);

        while (state != Thread.State.TERMINATED) {

            // 只要【线程】不终止，就一直【输出状态】
            thread.sleep(500);
                // 500 ms 延迟输出 1次, 不然输出特别快

            state = thread.getState();
                // 总共 有 5*1000 ms == TIMED_WAITING
                // 但 每1000mx 之间 穿插着 RUNNABLE

            System.out.println(state);

        }

        // TERMINATED 死亡后的 dead Thread 无法再次启动 .start()
//        thread.start(); // 直接报错
    }

}
